//  是否发送到附近以及标记标题的ctrl
//  SendSynchronousVC.m
//  SelfDriving
//
//  Created by wordtech on 2017/9/20.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "SendSynchronousVC.h"

@interface SendSynchronousVC ()
{
    BOOL isend;
    BOOL isSynchronous;
    NSString * title;
}

@property (strong, nonatomic) IBOutlet UIImageView *valueImage;

@property (strong, nonatomic) IBOutlet UIImageView *sendImage;
@property (strong, nonatomic) IBOutlet UIImageView *synchronousImage;

@end

@implementation SendSynchronousVC



- (void)viewDidLoad {
    [super viewDidLoad];
//    self.valueImage.contentMode = UIViewContentModeScaleAspectFit;
//    self.valueImage.image = _tpImge;
    self.valueImage.image = [self imageCompressForSize:_tpImge targetSize:[UIScreen mainScreen].bounds.size];
    //xhw2018-1-16

    
    isend = YES;
    isSynchronous = YES;
    
    //xhw2018-2-6加
    [self ref];
}

-(void)ref{
    [[AppDelegate shareDelegate]refdata:^(id res) {
        isend = [[AppDelegate shareDelegate].user.kuaipai_sendnearbyperson intValue]==0?NO:YES;
        
        isSynchronous = [[AppDelegate shareDelegate].user.kuaipai_videoshow intValue]==0?NO:YES;
        
        self.sendImage.image = isend ? [UIImage imageNamed:@"选择---已选.png"] : [UIImage imageNamed:@"选择---未选.png"];
        
        self.synchronousImage.image = isSynchronous ? [UIImage imageNamed:@"选择---已选.png"] : [UIImage imageNamed:@"选择---未选.png"];
        
    }];
    

}

//按比例缩放,size是你要把图显示到 多大区域 ,例如:CGSizeMake(300, 400)
-(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = size.width;
    CGFloat targetHeight = size.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    
    if(CGSizeEqualToSize(imageSize, size) == NO){
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
        }else{
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        if(widthFactor > heightFactor){
            thumbnailPoint.y = (targetHeight - scaledHeight) *0.5;
        }else if(widthFactor < heightFactor){
            thumbnailPoint.x = (targetWidth - scaledWidth) *0.5;
        }
    }
    
    UIGraphicsBeginImageContext(size);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    if(newImage == nil){
        NSLog(@"scale image fail");
    }
    UIGraphicsEndImageContext();
    return newImage;
}

// 返回popz
- (IBAction)backAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//编辑title
- (IBAction)editImageAction:(UIButton *)sender {
    
    UIAlertController * alertCtrl = [UIAlertController alertControllerWithTitle:@"标题" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertCtrl addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        //xhw改bug14931
        if (title) {
            textField.text = title;
        }
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField * titleTextField = alertCtrl.textFields.firstObject;
        title = titleTextField.text;
    }];
    [alertCtrl addAction:cancelAction];
    [alertCtrl addAction:okAction];
    [self presentViewController:alertCtrl animated:YES completion:nil];
}

//发布
- (IBAction)nextAction:(UIButton *)sender {
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    if (title) {
        [dic setObject:title forKey:@"title"];
    }
    else {
        [dic setObject:@"" forKey:@"title"];
    }
    
    //注意:NSMutableDictionary中不能保存bool值
    if (isend) {
        [dic setObject:@1 forKey:@"isSendNearBy"];
    }
    else {
        [dic setObject:@0 forKey:@"isSendNearBy"];
    }
    if (isSynchronous) {
        [dic setObject:@1 forKey:@"isSynchronizeToDynamic"];
    }
    else {
        [dic setObject:@0 forKey:@"isSynchronizeToDynamic"];
    }
    
    [self.delegate SendSynchronousVC:self param:dic];

}

//发送给附近和同步到我的动态
- (IBAction)sendSynchronousAction:(UIButton *)sender {
    if (sender.tag == 1001) {//发送给附近的人
        if (!isend) {

            
            self.sendImage.image = [UIImage imageNamed:@"选择---已选.png"];
        }else {
            self.sendImage.image = [UIImage imageNamed:@"选择---未选.png"];
        }
        
        //xhw2018-2-6加
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:isend?@"0":@"1"  forKey:@"kuaipai_sendnearbyperson"];
        [[BusinessService shareInstance]setUserOtherConfig:dic success:^(id result) {
            [self ref];
        } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
//        isend = !isend;
    }else if (sender.tag == 1002) {//同步动态
        if (!isSynchronous) {
            self.synchronousImage.image = [UIImage imageNamed:@"选择---已选.png"];
        }else {
            self.synchronousImage.image = [UIImage imageNamed:@"选择---未选.png"];
        }
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:isSynchronous?@"0":@"1"  forKey:@"kuaipai_videoshow"];
        [[BusinessService shareInstance]setUserOtherConfig:dic success:^(id result) {
            [self ref];
        } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
//        isSynchronous = !isSynchronous;
    }
}


@end









